package com.tanhua.server.exception;

import com.tanhua.model.domain.BaseResult;
import com.tanhua.model.vo.ErrorResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * 自定义统一异常处理
 * 1. 通过ControllerAdvice注解 声明异常处理类
 * 2.
 */
@ControllerAdvice
public class AdviceException {


    Log log = LogFactory.getLog(getClass());

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity handlerBusinessException(BusinessException be) {
        ErrorResult errorResult = be.getErrorResult();
        log.debug("捕获到BusinessException异常：" + errorResult.toString());
        return ResponseEntity.ok(BaseResult.builder().code(errorResult.getErrCode()).msg(errorResult.getErrMessage()).data(null).build());
    }


    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseEntity handlerMaxUploadSizeException(MaxUploadSizeExceededException e) {
        e.printStackTrace();
        log.debug("MaxUploadSizeExceededException：" + e.getMessage());
        return ResponseEntity.ok(BaseResult.builder().code(1).msg("文件太大啦，压一压呀").data(e.getMessage()).build());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity handlerOtherException(Exception e) {
        e.printStackTrace();
        log.debug("捕获到Exception异常：" + e.getMessage());
        return ResponseEntity.ok(BaseResult.builder().code(1).msg("未知错误").data(e.getMessage()).build());
    }


}
